﻿/**
//  DownloadCommand.cs
//  Marquee Moment
//  Copyright 2011 Systematix Technocrates. All rights reserved.
**/
using System;
using System.Runtime.InteropServices;
using EDSDKLib;
using MarqueeMoment.CameraController.Camera;
using MarqueeMoment.Forms;

namespace MarqueeMoment.CameraController.Command
{
    public class DownloadCommand : Command
    {

        private IntPtr directoryItem;

        public EDSDK.EdsProgressCallback inProgressCallback = new EDSDK.EdsProgressCallback(ProgressFunc);

        public DownloadCommand(CameraModel inModel, IntPtr dirItem)
            : base(inModel)
        {
            this.directoryItem = dirItem;
        }

        ~DownloadCommand() //protected override void Finalize() 23 December_2011
        {
            //// Release item.
            if ((this.directoryItem == null) == false)
            {
                EDSDK.EdsRelease(this.directoryItem);
                this.directoryItem = IntPtr.Zero;

            }
        }


        //// Execute a command .
        public override bool execute()
        {

            uint err = EDSDK.EDS_ERR_OK;
            IntPtr stream = IntPtr.Zero;

            //// Get informations of the downloadling directory item.
            EDSDK.EdsDirectoryItemInfo dirItemInfo = new EDSDK.EdsDirectoryItemInfo();
            err = EDSDK.EdsGetDirectoryItemInfo(this.directoryItem, out dirItemInfo);

            //// Notify starting transfer.
            if (err == EDSDK.EDS_ERR_OK)
            {
                frmTakePhoto.model.notifyObservers(commandName.strt,0);
            }

            //// Create a file stream for receiving image.
            if (err == EDSDK.EDS_ERR_OK)
            {
                err = EDSDK.EdsCreateFileStream(dirItemInfo.szFileName, EDSDK.EdsFileCreateDisposition.CreateAlways, EDSDK.EdsAccess.ReadWrite, out stream);
            }

            //// Set Progress Callback.

            if (err == EDSDK.EDS_ERR_OK)
            {
                GCHandle gch = GCHandle.Alloc(this);
                err = EDSDK.EdsSetProgressCallback(stream, inProgressCallback, EDSDK.EdsProgressOption.Periodically, (IntPtr)gch);
            }

            //// Download image

            if (err == EDSDK.EDS_ERR_OK)
            {
                err = EDSDK.EdsDownload(this.directoryItem, dirItemInfo.Size, stream);

            }

            //// Complete downloading

            if (err == EDSDK.EDS_ERR_OK)
            {
                err = EDSDK.EdsDownloadComplete(directoryItem);

            }

            //// Release item

            if ((this.directoryItem == null) == false)
            {
                err = EDSDK.EdsRelease(this.directoryItem);
                directoryItem = IntPtr.Zero;

            }

            //// Release the stream

            if ((stream == null) == false)
            {
                err = EDSDK.EdsRelease(stream);
                stream = IntPtr.Zero;

            }

            //// Notify to complete downloading 

            if (err == EDSDK.EDS_ERR_OK)
            {
                base.model.notifyObservers(commandName.cplt,0);

            }

            //// Notify Error
            if (err != EDSDK.EDS_ERR_OK)
            {
                base.model.notifyObservers(commandName.errr, err);
            }

            return true;

        }


        public static uint ProgressFunc(uint inPercent, IntPtr inContext, ref bool outCancel)
        {

            uint rtn = Convert.ToUInt32(EDSDK.EDS_ERR_OK);

          frmTakePhoto.model.notifyObservers(commandName.prog, (uint)inPercent);

            return rtn;

        }



    }
}
